package com.shansun.util
{
	import flash.utils.describeType;
	
	import mx.utils.ObjectUtil;

	public class BeanUtils
	{
		public function BeanUtils()
		{
		}

		public static function copyProperty(objTo:Object, objFrom:Object):void
		{
			if (objTo == null || objFrom == null)
				return;

			var claInfo:Object=ObjectUtil.getClassInfo(objTo);
			var props:Array=claInfo["properties"];
			for each (var q:QName in props)
			{
				try
				{
					if (objFrom.hasOwnProperty(q.localName))
					{
						objTo[q.localName]=objFrom[q.localName];
					}
				}
				catch (err:Error)
				{
				}
			}
		}
		
		public static function copyPropertyToClass(objTo:Object, objFrom:Object):void
		{
			if (objTo == null || objFrom == null)
				return;
 
			var description:XML = describeType(objTo);
			
			for each(var q:XML in description.elements('accessor')) {
				var node:Object = objFrom[q.@name];
				
				if(node != null) {
					if(q.@type == 'Date') {
						objTo[q.@name] = DateUtils.convertStrToDate(node as String);
					} else if(q.@type == 'Array') {
						objTo[q.@name] = node;
					} else {
						try {
							objTo[q.@name] = node;
						} catch(err:Error) {}
					}
				}
			}
		}

		public static function convertXMLList2Object(xml:XMLList, clazz:Class):Object
		{
			if (xml == null || clazz == null)
			{
				throw new Error("参数不合法！");
			}
			var ret:Object=new clazz();
			var claInfo:Object=ObjectUtil.getClassInfo(ret);
			var props:Array=claInfo["properties"];
			for each (var q:QName in props)
			{
				try
				{
					var node:XMLList=xml.child(q.localName);
					if (node != null)
					{
						ret[q.localName]=node.@value;
					}
				}
				catch (err:Error)
				{
					trace(err.getStackTrace());
				}
			}
			return ret;
		}

		public static function convertXML2Object(xml:XML, clazz:Class):Object
		{
			if (xml == null || clazz == null)
			{
				throw new Error("参数不合法！");
			}
			var ret:Object=new clazz();
			var claInfo:Object=ObjectUtil.getClassInfo(ret);
			var props:Array=claInfo["properties"];
			for each (var q:QName in props)
			{
				try
				{
					var node:XMLList=xml.child(q.localName);
					if (node != null)
					{
						ret[q.localName]=node.@value;
					}
				}
				catch (err:Error)
				{
					trace(err.getStackTrace());
				}
			}
			return ret;
		}

		public static function json2Object(jsonStr:String, clazz:Class):Object
		{
			if (jsonStr == null || clazz == null)
			{
				throw new Error("参数不合法！");
			}

			var jsonObj:Object=JsonUtil.deserialize(jsonStr);
			var ret:Object=new clazz();
			var claInfo:Object=ObjectUtil.getClassInfo(ret);
//			var props:Array=claInfo["properties"];
//
//			for each (var q:XML in description.elements('accessor'))
//			{
//				var qType:String=q.@type;
//				var nodeObj:Object=jsonObj[q.localName];
//				if (nodeObj != null)
//				{
//					if (qType == 'Date')
//					{
//						ret[q.@name]=DateUtils.convertStrToDate(nodeObj);
//					}
//					else if (qType == 'Array')
//					{
//						//TODO: 如果是数据，如何处理
//					}
//				}
//			}
			return ret;
		}
	}
}